<!--
 * @Descripttion: 部门信息编辑与新增dialog
 * @version: 
 * @Author: 淏
 * @Date: 2024-04-30 13:09:04
 * @LastEditors: 淏
 * @LastEditTime: 2024-04-30 19:10:04
-->
<template>
  <!-- 新增与编辑dialog -->
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="dialogClose"
    destroy-on-close
    width="25%"
  >
    <div slot="title" class="dialog-footer">
      <span class="text">{{ dialogTitle }}</span>
    </div>
    <el-form style="margin-top: 16px">
      <el-form-item label-width="150px">
        <span slot="label">
          <span class="text">部门名称</span>
        </span>
        <el-input
          style="width: 150px"
          size="mini"
          placeholder="请输入部门名称"
          v-model="formData.departmentName"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        size="mini"
        class="button"
        plain
        @click="dialogVisible = false"
      >
        取 消
      </el-button>
      <el-button
        size="mini"
        class="button"
        type="primary"
        plain
        @click="handleDialog"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { update_department, add_department } from '@/api/index.js'
  export default {
    // 组件名称
    name: 'department',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 局部注册的组件
    components: {},
    // 组件状态值
    data() {
      return {
        dialogTitle: '',
        dialogType: '',
        chooseData: '',
        dialogVisible: false,
        departments: [],
        formData: {
          departmentName: '',
          departmentId: '',
        },
      }
    },
    // 计算属性
    computed: {},
    // 侦听器
    watch: {},
    // 组件方法
    methods: {
      showDialog(type, chooseData) {
        this.dialogVisible = true
        if (type == 'update') {
          this.formData.departmentName = chooseData.name
          this.formData.departmentId = chooseData.id
          this.dialogTitle = '编辑部门信息'
          this.dialogType = 'update'
        } else {
          this.dialogTitle = '新增部门信息'
          this.dialogType = 'add'
        }
      },
      dialogClose(done) {
        this.$confirm('确认关闭？')
          .then((_) => {
            done()
            this.clearDialog()
          })
          .catch((_) => {})
      },
      clearDialog() {
        this.dialogVisible = false
        this.formData = {
          departmentName: '',
          departmentId: '',
        }
      },
      handleDialog() {
        if (this.dialogType == 'add') {
          this.addData()
        } else {
          this.updateData()
        }
      },
      addData() {
        let departmentName = this.formData.departmentName
        var qs = require('qs')
        var data = qs.stringify({
          departmentName: departmentName,
        })
        add_department(data).then((res) => {
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message,
          })
          this.$emit('getTableData')
          this.clearDialog()
        })
      },
      updateData() {
        let departmentId = this.formData.departmentId
        let departmentName = this.formData.departmentName
        var qs = require('qs')
        var data = qs.stringify({
          departmentId: departmentId,
          departmentName: departmentName,
        })
        update_department(data).then((res) => {
          console.log(res)
          this.$message({
            type: 'success',
            message: res.message,
          })
          this.$emit('getTableData')
          this.clearDialog()
        })
      },
    },
    created() {},
    mounted() {},
  }
</script>

<style scoped>
  .text {
    color: #00ffff;
    font-weight: 500;
  }
  .button {
    background: rgba(0, 0, 255, 0.2);
    border-radius: 10px;
    border: 1px solid #295a85;
    color: #00ffff;
    text-shadow: 0 0 25px #00ffff;
    font-weight: 700;
  }
  .handle-select {
    width: 200px;
  }
</style>
<style lang="less" scoped>
  /deep/.el-dialog {
    border-radius: 5px;
    background: rgba(48, 89, 224, 0.8);
  }
  /deep/.el-dialog__headerbtn .el-dialog__close {
    color: #ffffff;
  }
</style>
